package com.app.qbake.module.video.adapter;

import android.app.Activity;
import android.support.annotation.NonNull;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.hannesdorfmann.adapterdelegates3.AdapterDelegate;
import com.wind.base.adapter.DisplayItem;
import com.wind.base.utils.ViewInflater;
import com.wind.base.widget.WidthRatioFrameLayout;
import com.wind.data.square.bean.Goods;
import com.app.qbake.R;
import com.app.qbake.module.video.event.VideoItemClickEvent;

import org.greenrobot.eventbus.EventBus;

import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

public class VideoDelegate extends AdapterDelegate<List<DisplayItem>> {

    private Activity mActivity;
    private String mType;
    public VideoDelegate(Activity activity){
        this.mActivity=activity;
    }
    @Override
    protected boolean isForViewType(@NonNull List<DisplayItem> items, int position) {
        return items.get(position) instanceof Goods;
    }

    @NonNull
    @Override
    protected RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent) {
        View itemView=mActivity.getLayoutInflater().inflate(R.layout.item_video,parent,false);
        return new ViewHolder(itemView);
    }

    @Override
    protected void onBindViewHolder(@NonNull List<DisplayItem> items, int position, @NonNull RecyclerView.ViewHolder holder, @NonNull List<Object> payloads) {
        ViewHolder vh= (ViewHolder) holder;
        final Goods item= (Goods) items.get(position);
        ViewInflater.inflateImage(vh.iv,item.getVideo_image());

        ViewInflater.inflateText(vh.tv_name,item.getUserName());
        ViewInflater.inflateText(vh.tv_play_count,item.getHits()+"");
        ViewInflater.inflateText(vh.tv_time,item.getCreated_at()+"发布");

        if (position%2==0){
            vh.rfl.setmRatio(1.6f);
        }else {
            vh.rfl.setmRatio(1.2f);
        }

        vh.itemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                EventBus.getDefault().post(new VideoItemClickEvent(item,mType));
            }
        });
    }

    public void setType(String type) {
        mType=type;
    }

    static class ViewHolder extends RecyclerView.ViewHolder{
        @BindView(R.id.iv)
        ImageView iv;
        @BindView(R.id.tv_name)
        TextView tv_name;
        @BindView(R.id.tv_play_count)
        TextView tv_play_count;
        @BindView(R.id.tv_time)
        TextView tv_time;
        @BindView(R.id.rfl)
        WidthRatioFrameLayout rfl;
        public ViewHolder(View itemView) {
            super(itemView);
            ButterKnife.bind(this,itemView);
        }
    }
}
